import cookieParser from 'cookie-parser';
import express from 'express';
import logger from 'morgan';
import path from 'path';
import * as rfs from 'rotating-file-stream'; // version 2.x
// import bodyParser from 'body-parser'; // 添加缺少的 bodyParser 导入
import { Config } from './src/config/Config';
import indexRouter from './src/routes/index';

const app = express();
// 设置 CORS 头信息
app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*'); // 允许所有来源的请求
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE,OPTIONS'); // 允许的 HTTP 方法
  res.header('Access-Control-Allow-Headers', '*'); // 允许的请求头
  next();
});
//系统日志
var accessLogStream = rfs.createStream('access.log', {
  interval: '1d', // rotate daily
  path: path.join(__dirname, '..', './logs/log')
})
app.use(logger(':remote-addr :method :url :status :res[content-length] - :response-time ms', { stream: accessLogStream }));
// app.use(bodyParser.json({limit: '10mb', extended: true})); 
app.use(express.json({ limit: '150mb' }));
app.use(express.urlencoded({ limit: '150mb', extended: false }));
app.use(express.static(path.join(__dirname, '..', 'static')));
app.use(cookieParser());



//注册路由
app.use('/', indexRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  res.status(404).send({
    code: 404,
    msg: '404 Not Found'
  });
});

app.listen(Config.PORT, () => {
  console.log(`Server is running on http://localhost:${Config.PORT}`);
});

module.exports = app;